package com.apartment.own.controller;

import java.util.List;

import com.apartment.own.request.ord.ApproveDTO;
import com.apartment.own.request.ord.ContractTemplateDTO;
import com.apartment.own.request.ord.TenantContractDTO;
import com.apartment.poi.TenantContractTools;
import com.apartment.poi.domin.ContractTemplate;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.apartment.common.annotation.Log;
import com.apartment.common.core.controller.BaseController;
import com.apartment.common.core.domain.AjaxResult;
import com.apartment.common.enums.BusinessType;
import com.apartment.own.domain.OwnOrdTenantContract;
import com.apartment.own.service.IOwnOrdTenantContractService;
import com.apartment.common.utils.poi.ExcelUtil;
import com.apartment.common.core.page.TableDataInfo;

/**
 * 租客合同Controller
 *
 * @author @Dog_E
 * @date 2020-08-28
 */
@RestController
@RequestMapping("/own/contract")
@Api(value = "租客合同", tags = "租客合同")
public class OwnOrdTenantContractController extends BaseController {
    @Autowired
    private IOwnOrdTenantContractService ownOrdTenantContractService;

    /**
     * 查询租客合同列表
     */
    @PreAuthorize("@ss.hasPermi('own:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(OwnOrdTenantContract ownOrdTenantContract) {
        startPage();
        List<OwnOrdTenantContract> list = ownOrdTenantContractService.selectOwnOrdTenantContractList(ownOrdTenantContract);
        return getDataTable(list);
    }

    /**
     * 导出租客合同列表
     */
    @PreAuthorize("@ss.hasPermi('own:contract:export')")
    @Log(title = "租客合同", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnOrdTenantContract ownOrdTenantContract) {
        List<OwnOrdTenantContract> list = ownOrdTenantContractService.selectOwnOrdTenantContractList(ownOrdTenantContract);
        ExcelUtil<OwnOrdTenantContract> util = new ExcelUtil<OwnOrdTenantContract>(OwnOrdTenantContract.class);
        return util.exportExcel(list, "contract");
    }

    /**
     * 获取租客合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('own:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ownOrdTenantContractService.selectOwnOrdTenantContractById(id));
    }

//    /**
//     * 新增租客合同
//     */
//    @PreAuthorize("@ss.hasPermi('own:contract:add')")
//    @Log(title = "租客合同", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody OwnOrdTenantContract ownOrdTenantContract)
//    {
//        return toAjax(ownOrdTenantContractService.insertOwnOrdTenantContract(ownOrdTenantContract));
//    }


    /**
     * @param contractDTO: 添加租客合同
     * @Description:新增租客合同 pc
     * @return: com.apartment.common.core.domain.AjaxResult
     * @Author: @Dog_E
     * @Date: 2020/8/31 0031
     **/
    @PreAuthorize("@ss.hasPermi('own:contract:add')")
    @Log(title = "租客合同", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("添加租客合同")
    public AjaxResult pcAdd(@RequestBody TenantContractDTO contractDTO) {
        return toAjax(ownOrdTenantContractService.insertTenantContract(contractDTO));
    }

    /**
     * 修改租客合同
     */
    @PreAuthorize("@ss.hasPermi('own:contract:edit')")
    @Log(title = "租客合同", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OwnOrdTenantContract ownOrdTenantContract) {
        return toAjax(ownOrdTenantContractService.updateOwnOrdTenantContract(ownOrdTenantContract));
    }

    /**
     * 删除租客合同
     */
    @PreAuthorize("@ss.hasPermi('own:contract:remove')")
    @Log(title = "租客合同", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ownOrdTenantContractService.deleteOwnOrdTenantContractByIds(ids));
    }

    /**
     * @Description: 合同预览
     * @return: void
     * @Author: @Dog_E
     * @Date: 2020/9/9 0009 11:46
     **/
    public void contractPreview(@RequestBody ContractTemplateDTO contractTemplateDTO) {
        ContractTemplate contractTemplate = new ContractTemplate();
        BeanUtils.copyProperties(contractTemplateDTO, contractTemplate);
        TenantContractTools.generateContract(contractTemplate);
    }


    /**
     * @param approveDTO: 审查查询
     * @Description: 审查查询
     * @return: com.apartment.common.core.domain.AjaxResult
     * @Author: @Dog_E
     * @Date: 2020/9/14 0014 17:20
     **/
    @PreAuthorize("@ss.hasPermi('own:contract:list')")
    @GetMapping("/approve")
    @ApiOperation("审查查询")
    public AjaxResult approve(@RequestBody ApproveDTO approveDTO) {
        return AjaxResult.success(ownOrdTenantContractService.approvalQuery(approveDTO));
    }
}
